/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.dat076.datesite.controllers;

import edu.chl.dat076.datesite.ejbs.UserEJBLocal;
import edu.chl.dat076.datesite.entities.SiteUser;
import java.io.Serializable;
import java.util.List;
import java.util.Vector;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.SelectEvent;

/**
 *
 * @author Mikey
 */
@ManagedBean
@ViewScoped
public class SearchResultsBackingBean implements Serializable {

    @EJB
    private UserEJBLocal userJPACtrl;
    
    private List<SiteUser> users;

    private SiteUser selectedUser;
    
    private SiteUser me;

    /**
     * TODO: feed users to this bean instead of getting
     * them from the db
     */
   
    @PostConstruct
    public void postConstruct (){
        setUsers((Vector)FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("results"));
        setMe(userJPACtrl.getUser(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user").toString()));
        users.remove(me);
    }
        
    public List<SiteUser> getUsers() {
     try {
            System.out.println("Getting users: "+users.size());
            return users;
     }
     catch (Exception e) {
            System.out.println("EXCEPTION WHILE GETTING USERS!");
            users = new Vector<SiteUser>();
            }
     return users;
     }

    public void setUsers(List<SiteUser> users) {
        this.users = users;
    }
    
    public void setSelectedUser(SiteUser selectedUser) {
        this.selectedUser = selectedUser;
    }
    
    public SiteUser getSelectedUser() {
        return selectedUser;
    }

    public String navigate(SelectEvent event) {
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("userProfile", event.getObject());  
        return "dateprofile.jsf?faces-redirect=true";
    }

    private void setMe(SiteUser siteUser) {
        this.me = siteUser;
    }

    public SiteUser getMe() {
        return me;
    }
}